﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.UI.HtmlControls;

namespace KMFrame.Util
{
    public class HtmlHelper
    {
        public static void BindHtmlTable(HtmlGenericControl tbody, IEnumerable<object> list, string keyname, string keytag)
        {
            foreach (object o in list)
            {
                PropertyInfo key = o.GetType().GetProperty(keyname);
                object kv = key.GetValue(o, null);
                string ks = (kv == null) ? "" : kv.ToString();

                HtmlTableRow row = new HtmlTableRow();
                row.Attributes["target"] = keytag;
                row.Attributes["rel"] = ks;

                foreach (PropertyInfo f in o.GetType().GetProperties())
                {
                    object ro = f.GetValue(o, null);
                    string rs = (ro == null) ? "" : ro.ToString();
                    HtmlTableCell cell = new HtmlTableCell();
                    cell.InnerText = rs;
                    row.Controls.Add(cell);
                }
                tbody.Controls.Add(row);
            }
        }
    }
}
